package my_web.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import dao.dao.impl.DaoFactory;
import my_web.Interface.DeliverMissionInterface;
import my_web.bean.DeliverMission;
import my_web.bean.Emp;
import my_web.model.DeliverMissionDto;
import my_web.page.Page;
import my_web.page.Imp.PageEngineFactory;
public class DeliverMissionservice implements DeliverMissionInterface{
	public Page DynamicQueryAllDeliverMissions(DeliverMissionDto dto, Emp emp) {
		StringBuilder where=new StringBuilder();
		List<Object> paramList=new ArrayList<>();
		String sql = null;
		//sql="select D.m_report,D.e_id,D.m_report_date,D.d_state,D.m_check_result,D.m_check_date, M.m_id, M.m_name, M.m_content, M.m_end_date,E.e_name from deliver_table D left join mission_table M on D.m_id=M.m_id left join Emp_table E on D.e_id=E.e_id where 1=1";
		if(emp.getP_name().equals("Boss")){//boss--查看本部门所有员工的任务记录
			sql="select D.m_report,D.e_id,D.m_report_date,D.d_state,D.m_check_result,D.m_check_date,M.m_id,M.m_name, M.m_content, M.m_end_date,E.e_name,E.d_id from deliver_table D left join mission_table M on D.m_id=M.m_id left join Emp_table E on D.e_id=E.e_id where E.d_id=?";
			paramList.add(0,emp.getD_id());
		}else{//普通员工:只能查看自己的任务记录
			sql="select D.m_report,D.e_id,D.m_report_date,D.d_state,D.m_check_result,D.m_check_date,M.m_id,M.m_name, M.m_content, M.m_end_date,E.e_name,E.d_id from deliver_table D left join mission_table M on D.m_id=M.m_id  left join Emp_table E on D.e_id=E.e_id where D.e_id=?";
			paramList.add(0,emp.getE_id());
		}
		if(dto!=null){
			if(dto.getM_id()!=null && !dto.getM_id().equals("")){
				where.append(" and m_id like ?");
				paramList.add("%"+dto.getM_id()+"%");
			}
			if(dto.getE_id()!=null && !dto.getE_id().equals("")){
				where.append(" and e_id like ?");
				paramList.add("%"+dto.getE_id()+"%");
			}
			if(dto.getM_report()!=null && !dto.getM_report().equals("")){
				where.append(" and m_report like ?");
				paramList.add("%"+dto.getM_report()+"%");
			}
			if(dto.getM_end_date()!=null && !dto.getM_end_date().equals("")){
				where.append(" and m_end_date>=?");
				paramList.add(dto.getM_end_date());
			}
			if(dto.getM_report()!=null && !dto.getM_report().equals("")){
				where.append(" and m_report like ?");
				paramList.add("%"+dto.getM_report()+"%");
			}
			if(dto.getM_report_date()!=null && !dto.getM_report_date().equals("")){
				where.append(" and m_report_date<=?");
				paramList.add(dto.getM_report_date());
			}
			if(dto.getM_check_result()!=null && !dto.getM_check_result().equals("")){
				where.append(" and m_check_result like?");
				paramList.add("%"+dto.getM_check_result()+"%");
			}
			if(dto.getM_check_date()!=null && !dto.getM_check_date().equals("")){
				where.append(" and m_check_date<=?");
				paramList.add(dto.getM_check_date());
			}
			if(dto.getM_content()!=null && !dto.getM_content().equals("")){
				where.append(" and m_content like ?");
				paramList.add("%"+dto.getM_content()+"%");
			}
			if(dto.getM_name()!=null && !dto.getM_name().equals("")){
				where.append(" and m_name like ?");
				paramList.add("%"+dto.getM_name()+"%");
			}
		}
		where=where.append("order by m_id");
		try{
			return PageEngineFactory.createOraclePage(DeliverMission.class, DaoFactory.getCurrDao(), sql + where, dto, paramList.toArray());
		}catch(RuntimeException e){
			throw e;
		}
	}
	public DeliverMissionDto feedbackMission(String m_id) {
		return DaoFactory.getCurrDao().queryBean(DeliverMissionDto.class,
				"select D.*,M.m_id,M.m_name,M.m_content,M.m_end_date from deliver_table D left outer join mission_table M on D.m_id=M.m_id where D.m_id=?",m_id);
	}
	public void getFeedback(DeliverMissionDto dto) {
		DaoFactory.getCurrDao().update("update deliver_table set m_report=?,m_report_date=?,d_state=? where m_id=? and e_id=?",dto.getM_report(),new Date(),"已汇报",dto.getM_id(),dto.getE_id());
		DaoFactory.getCurrDao().update("update mission_table set m_state=? where m_id=?","已汇报",dto.getM_id());	
	}
	public DeliverMissionDto checkMission(String m_id) {
		return DaoFactory.getCurrDao().queryBean(DeliverMissionDto.class,
				"select D.*,M.m_id,M.m_name,M.m_content,M.m_end_date from deliver_table D left outer join mission_table M on D.m_id=M.m_id where D.m_id=? ",m_id);
	}
	public void getMissionCheck(DeliverMissionDto dto) {
		DaoFactory.getCurrDao().update("update deliver_table set m_check_result=?,m_check_date=?,d_state=? where m_id=?",
				dto.getM_check_result(), new Date(), "已汇报", dto.getM_id());
		DaoFactory.getCurrDao().update("update mission_table set m_state=?","已完成");
	}
	public void getAbandonMission(String m_id) {
		DaoFactory.getCurrDao().update("update mission_table set m_state=? where m_id=?","已取消",m_id);
		DaoFactory.getCurrDao().update("update deliver_table set d_state=? where m_id=?","已取消",m_id);	
	}
}
